package cn.tx;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ToolsPanel extends JPanel {
    public ToolsPanel(){
        init();
    }
    //定义一个面板
    JPanel panel = new JPanel();
    String tool;//定义使用的工具
    public void init(){
        //定义流式布局
        this.setLayout(new FlowLayout(FlowLayout.LEFT));
        //panel面板上的布局方式，网格布局
        panel.setLayout(new GridLayout(5,2,4,4));
        //向网格里面添加组件
        String[] array = {"images/HollowCircle.png",
                "images/HollowRec.png",
                "images/HollowTri.png",
                "images/line.png",
                "images/pen.png",
                "images/point.png",
                "images/rubber.png",
                "images/SolidCircle.png",
                "images/SolidRec.png",
                "images/SolidTri.png"};
        for(int i=0;i<array.length;i++){
            //创建图片对象
            ImageIcon image = new ImageIcon(array[i]);
            //创建按钮
            JButton button = new JButton(image);
            //设置按钮大小
            button.setPreferredSize(new Dimension(50,50));
            //设置按钮的命令
            button.setActionCommand(array[i].substring(7,array[i].lastIndexOf(".")));
            //给按钮添加事件
            button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    //获取这个命令
                    tool = e.getActionCommand();
                    System.out.println("使用工具："+tool);
                }
            });
            //添加到panel
            panel.add(button);
        }
        this.add(panel);
    }
}
